package com.woniuxy.controller;

import com.woniuxy.pojo.Customer;
import com.woniuxy.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("cus")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @RequestMapping("goList.html")
    public String goList(){
        return "cus_goList";
    }
    @RequestMapping("findAll.html")
    @ResponseBody
    public List<Customer> findAll(String name){
        return customerService.findAll(name);
    }
    @RequestMapping("goAdd.html")
    public String goAdd(){
        return "cus_add";
    }
    @RequestMapping("add.html")
    @ResponseBody
    public String save(@RequestBody Customer customer){
        System.out.println(customer);
        Boolean flag = customerService.save(customer);
        String msg = flag?"success":"fail";
        return msg;
    }
    @RequestMapping("del.html")
    @ResponseBody
    public String del(Integer id){
        Boolean flag = customerService.del(id);
        String msg = flag?"success":"fail";
        return msg;
    }
    @RequestMapping("goUpd.html")
    public String goUpd(Integer id,Model model){
        model.addAttribute("cusId",id);
        return "cus_upd";
    }
    @RequestMapping("findOne.html")
    @ResponseBody
    public Customer findOne(Integer id){
        return customerService.findOne(id);
    }
    @RequestMapping("update.html")
    @ResponseBody
    public String update(@RequestBody Customer customer){
        Boolean flag = customerService.update(customer);
        String msg = flag?"success":"fail";
        return msg;
    }
}
